package com.qf.shopping.servlet.filter;

import com.qf.shopping.common.StaticResource;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**过滤请求页面是否需要设置编码
 * @author 三旬
 * @version 1.0  2022/9/13
 */
@WebFilter("/*")
public class EncodingFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //1.类型转化
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //设置白名单
        //获得请求地址
        String uri = request.getRequestURI();
        if (StaticResource.checkUrl(uri)) {
        }else {
            System.out.println("设置编码的地址："+uri);
            //2.设置编码
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
        }
        //3.放行
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }


}
